﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.filters.BevelFilter
	import flash.display.Sprite;
	import flash.display.StageQuality;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author Peter Tolstrup
	 */
	public final class Main extends Sprite 
	{
		private static const WIDTH:int = 640;
		private static const HEIGHT:int = 420;
		
		private var _drawShape:Shape;
		
		private var _drawBitmapData:BitmapData;
		private var _drawBitmap:Bitmap;
		
		private var _noiseBitmapData:BitmapData;
		private var _noiseOffset:Array;
		private var _noiseOffsetSpeed1:Number;
		private var _noiseOffsetSpeed2:Number;
		
		private var _noise2BitmapData:BitmapData;
		private var _noise2Offset:Array;
		private var _noise2OffsetSpeed1:Number;
		private var _noise2OffsetSpeed2:Number;
		
		private var stepSize:int = 20;
		private var stepWidth:int = WIDTH / stepSize;
		private var stepHeight:int = HEIGHT / stepSize;
		
		private var radians:Number;
		private var radius:Number;
		private var xcoordinate:int;
		private var ycoordinate:int;					
		private var i:int = 0;
		private var j:int = 0;
		
		public function Main():void 
		{
			//stage.quality = StageQuality.LOW;
			
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			_drawShape = new Shape();
			
			_drawBitmapData = new BitmapData(WIDTH, HEIGHT);
			_drawBitmap = new Bitmap(_drawBitmapData);
			this.addChild(_drawBitmap);
			
			_noiseBitmapData = new BitmapData(stepWidth,stepHeight);			
			_noiseOffset = [new Point(), new Point()];
			_noiseOffsetSpeed1 = 0.02;
			_noiseOffsetSpeed2 = -0.02;
						
			_noise2BitmapData = new BitmapData(stepWidth,stepHeight);				
			_noise2Offset = [new Point(), new Point()];
			_noise2OffsetSpeed1 = 0.2;
			_noise2OffsetSpeed2 = -0.2;
			
			addEventListener(Event.ENTER_FRAME, update);
		}		
			
		private static const rect:Rectangle = new Rectangle(0, 0, WIDTH, HEIGHT);
		private static const colorTransform1:ColorTransform = new ColorTransform(1, 1, 1, 0.2);
		private static const colorTransform2:ColorTransform = new ColorTransform(1, 1, 1, 0.9);
		
		
		
		
		private function update(e:Event):void 
		{
			
			_drawBitmapData.colorTransform( rect, colorTransform1 );
			
			_noiseOffset[0].x += _noiseOffsetSpeed1;
			_noiseOffset[0].y += _noiseOffsetSpeed1;			
			_noiseOffset[1].x += _noiseOffsetSpeed2;
			_noiseOffset[1].y += _noiseOffsetSpeed2;			
			_noiseBitmapData.perlinNoise(stepWidth, stepHeight, 3, 1, false, false, 1, true, _noiseOffset);		
			
			_noise2Offset[0].x += _noise2OffsetSpeed1;
			_noise2Offset[0].y += _noise2OffsetSpeed1;			
			_noise2Offset[1].x += _noise2OffsetSpeed2;
			_noise2Offset[1].y += _noise2OffsetSpeed2;			
			_noise2BitmapData.perlinNoise(stepWidth, stepHeight, 5, 2, false, false, 1, true, _noise2Offset);	
			
			_drawShape.graphics.clear();
			_drawShape.graphics.lineStyle(1);
			i = 0;
			var a:int = 0;
			var b:int = 0;
			for (i ; i < WIDTH; i += stepSize )
			{
				j = 0;
				b = 0;
				for (j; j < HEIGHT; j += stepSize )
				{	
					radians = _noiseBitmapData.getPixel(a, b) / Number(0xffffff) * Math.PI * 2;
					radius = _noise2BitmapData.getPixel(a, b) / 0xffffff * stepSize * 2;
					xcoordinate = radius * Math.cos(radians);
					ycoordinate = radius * Math.sin(radians);
					
					_drawShape.graphics.moveTo(i , j );
					_drawShape.graphics.lineTo(i  + xcoordinate, j + ycoordinate);
					b++;
				}
				a++;
			}
			
			_drawBitmapData.draw(_drawShape, null, colorTransform2);
		}	

	}	
}